import { createRouter, createWebHistory } from 'vue-router'
const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/login/index.vue")
  },
  {
    path: '/',
    name: 'layout',
    meta: {
      title: '首页',
    },
    component: () => import('@/views/layout/index.vue'),
    redirect: { name: 'home' },
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          isAffix: true,
        },
        component: () => import('@/views/home/index.vue')
      },
      // 系统管理
      {
        path: '/system',
        name: 'system',
        redirect: { name: 'user' },
        component: () => import('@/views/system/index.vue'),
        meta: {
          title: '系统管理'
        },
        children: [
          {
            path: 'user',
            name: 'user',
            meta: {
              title: '用户管理'
            },
            component: () => import('@/views/system/user.vue')
          },
          {
            path: 'role',
            name: 'role',
            meta: {
              title: '角色管理'
            },
            component: () => import('@/views/system/role.vue')
          },
          {
            path: 'menu',
            name: 'menu',
            meta: {
              title: '菜单管理'
            },
            component: () => import('@/views/system/menu.vue')
          }
        ]
      },
      //用户信息
      {
        path: '/user',
        name: 'userInfo',
        meta: {
          title: '用户信息'
        },
        redirect: { name: 'info' },
        component: () => import('@/views/user/index.vue'),
        children: [
          {
            path: 'info',
            name: 'info',
            meta: {
              title: '个人信息修改'
            },
            component: () => import('@/views/user/userInfo.vue')
          },
          {
            path: 'password',
            name: 'password',
            meta: {
              title: '密码修改'
            },
            component: () => import('@/views/user/password.vue')
          },
        ]
      },
      //流程管理
      {
        path: '/flow',
        name: 'flow',
        redirect: { name: 'customizeForm' },
        component: () => import('@/views/flow/index.vue'),
        meta: {
          title: '流程管理'
        },
        children: [
          {
            path: 'customizeForm',
            name: 'customizeForm',
            meta: {
              title: '表单设计'
            },
            component: () => import('@/views/flow/CutomizeForm/index.vue')
          },
          {
            path: 'customizeFlow',
            name: 'customizeFlow',
            meta: {
              title: '流程设计'
            },
            component: () => import('@/views/flow/CustomizeFlow/index.vue')
          },
          {
            path: 'flowList',
            name: 'flowList',
            meta: {
              title: '流程定义'
            },
            component: () => import('@/views/flow/flowList/index.vue')
          },
          {
            path: 'formList',
            name: 'formList',
            meta: {
              title: '表单管理'
            },
            component: () => import('@/views/flow/formList/index.vue')
          },

        ]
      },
      //我的流程
      {
        path: '/myFlow',
        name: 'myFlow',
        redirect: { name: 'initialization' },
        component: () => import('@/views/myFlow/index.vue'),
        meta: {
          title: '我的流程'
        },
        children: [
          {
            path: 'initialization',
            name: 'initialization',
            meta: {
              title: '发起流程'
            },
            component: () => import('@/views/myFlow/initialization/index.vue')
          },
          {
            path: 'myInitialization',
            name: 'myInitialization',
            meta: {
              title: '我发起的'
            },
            component: () => import('@/views/myFlow/myInitialization/index.vue')
          },
          {
            path: 'pendingGet',
            name: 'pendingGet',
            meta: {
              title: '待我领取'
            },
            component: () => import('@/views/myFlow/pendingGet/index.vue')
          },
          {
            path: 'pendingApproval',
            name: 'pendingApproval',
            meta: {
              title: '待我审批'
            },
            component: () => import('@/views/myFlow/pendingApproval/index.vue')
          },
          {
            path: 'completeApproval',
            name: 'completeApproval',
            meta: {
              title: '我已审批'
            },
            component: () => import('@/views/myFlow/completeApproval/index.vue')
          }

        ]
      },
      {
        path: '/data',
        name: 'data',
        component: () => import('@/views/data/index.vue'),
        meta: {
          title: '数据管理'
        },
        redirect: { name: 'allFlow' },
        children: [
          {
            path: 'allFlow',
            name: 'allFlow',
            meta: {
              title: '全部流程'
            },
            component: () => import('@/views/data/allFlow/index.vue')
          },
          {
            path: 'statistic',
            name: 'statistic',
            meta: {
              title: '数据统计'
            },
            component: () => import('@/views/data/statistic/index.vue')
          },
        ]
      },


    ]
  },
  {
    //捕获404，not found
    path: '/:catchAll(.*)',
    name: 'NotFound',
    component: () => import("@/views/layout/notFound.vue")
  },

]



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})




/**
 * 重置路由
 */
export function resetRouter () {
  router.replace({ path: '/login' });
  location.reload();
}

//路由前置钩子
router.beforeEach((to, from, next) => {
  let token = ''

  //刷新页面重新获取缓存的数据
  if (localStorage.getItem('accessToken') && localStorage.getItem('accessToken') !== 'null') {
    token = localStorage.getItem('accessToken')
  } else if (sessionStorage.getItem('accessToken') && sessionStorage.getItem('accessToken') !== 'null') {
    token = sessionStorage.getItem('accessToken')
  }

  if (!token && to.name !== 'login') {
    next({ name: 'login' })
  } else {
    next()
  }
  // ...
  // 返回 false 以取消导航
})



export default router
